<?php
class m_collecting_day extends Model{
    var $table = "collecting_day";
    var $tableDetail = "detail_collecting_day";
    
    function m_collecting_day(){
        parent::Model();
    }
    function addCollectingDay($kegiatan,$lokasi,$idDropzone,$jumlah,$tanggal){
        $data = array('kegiatan' => $kegiatan,
            'tanggal' => $tanggal,
            'lokasi' => $lokasi,
            'id_member' => $this->session->userdata('id_member'),
            'status' => '0');
        $insert = $this->db->insert($this->table,$data);
        $id = $this->db->insert_id();
        
        $i = 0;
        foreach ($idDropzone as $row){
            if($row != "" && isset ($row)){
                $detail = array('id_collecting_day' => $id,
                    'id_dropzone' => $row,
                    'jumlah' => currencyToNumber($jumlah[$i]));
                $detailInsert = $this->db->insert($this->tableDetail,$detail);
                $i++;
            }
        }
        if($insert){
            $return['status'] = TRUE;
            $return['id'] = $id;
            return $return;
        }else return FALSE;
    }
    function detailCollectingDay($id){
        $master = $this->db->query("select c.*,m.nama as member from collecting_day c join member m on c.id_member = m.id where c.id = '$id'");
        if($master->num_rows() > 0){
            $return['master'] = $master->result_array();
        }else $return['master'] = array();
        
        $detail = $this->db->query("select dc.jumlah,d.nama as dropzone from detail_collecting_day dc join dropzone d on dc.id_dropzone = d.id where dc.id_collecting_day = '$id'");
        if($detail->num_rows() > 0){
            $return['detail'] = $detail->result_array();
        }else $return['detail'] = array();
        
        return $return;
    }
    function getCountRows($params){
        $where = "";
        if($params['awal'] != "" && $params['akhir']){
            $where .= "where tanggal between '$params[awal]' and '$params[akhir]'";
        }
        $query = $this->db->query("select * from collecting_day $where");
        
        return $query->num_rows();
    }
    function getListCollectingDay($offset = NULL,$limit = NULL,$params = NULL){
        $where = "";
        if($params['awal'] != "" && $params['akhir']){
            $where .= "where c.tanggal between '$params[awal]' and '$params[akhir]'";
        }
        if($offset != NULL || $limit != NULL){
            $limits = "limit $offset,$limit";
        }else $limits = '';
        $query = $this->db->query("select c.*,m.nama as member,(select sum(jumlah) from detail_collecting_day where id_collecting_day = c.id) as total from collecting_day c 
        join member m on c.id_member = m.id $where $limits");
        if($query->num_rows() > 0){
            return $query->result_array();
        }else return array();
    }
    function getCollectingDay($bulan = NULL,$tahun = NULL){
        $tanggal = betweenDate($bulan, $tahun);
        $query = $this->db->query("select c.*,m.nama as member,(select sum(jumlah) from detail_collecting_day where id_collecting_day = c.id) as total from collecting_day c 
        join member m on c.id_member = m.id
        where c.tanggal between '$tanggal[awal]' and '$tanggal[akhir]'");
        
        if($query->num_rows() > 0){
            return $query->result_array();
        }else return array();
    }
    function unapproveCollectingDay($id,$bulan,$tahun,$jumlah){
        $updateRekap = $this->db->query("update rekap_keuangan_bulanan set jumlah = (jumlah-$jumlah) where id = '".$bulan.$tahun."'"); 
        $query = $this->db->query("update collecting_day set status = '0' where id = '$id'");
        if($query) return TRUE;
    }
}
?>
